package com.ama.controller;

import ch.qos.logback.classic.sift.AppenderFactoryUsingJoran;
import com.ama.dao.BedDao;
import com.ama.dao.RoomDao;
import com.ama.dao.StaffDao;
import com.ama.pojo.Bed;
import com.ama.pojo.Staff;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class GenderController {
    @Autowired
    BedDao bedDao;

    @Autowired
    StaffDao staffDao;

    @RequestMapping("/genderJudgment")
    public String genderJudgment(HttpServletRequest request, HttpSession session) {
        Long roomId = Long.valueOf(request.getParameter("room"));
        System.out.println(roomId);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("room_id", roomId);
        List<Bed> bedList = bedDao.selectList(wrapper);
        String gender = ((Staff) session.getAttribute("user")).getGender();
        System.out.println(gender);
        for (Bed bed : bedList) {

            //被占用
            if (bed.getBedState() != 1) {
                //当前申请人性别判断
                //如果申请人为男
//                if(gender.equals("男")){
                System.out.println(bed.getBedId());
                Long bedId = bed.getBedId();
                Staff staff = staffDao.selectStaffByBedId(bedId);
                System.out.println(staff);
                //如果当前申请人性别和床铺性别不同，返回失败
                if (!staff.getGender().equals(gender)) {
                    return "redirect:/error/apply_genderError.jsp";
                }
//                }
            }
        }
        request.setAttribute("room", roomId);
        return "redirect:/staff_page/s_bed.jsp?room=" + roomId;
    }
}
